import router from './router'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import useRouteCacheStore from './stores/modules/routeCache'
import type { EnhancedRouteLocation } from './router/types'
import useRouteTransitionNameStore from './stores/modules/routeTransitionName'

NProgress.configure({ showSpinner: false, parent: '#app', speed: 100 })
router.beforeEach((to: EnhancedRouteLocation, from, next) => {
  NProgress.start()

  const routeCacheStore = useRouteCacheStore()
  const routeTransitionNameStore = useRouteTransitionNameStore()

  // Route cache
  routeCacheStore.addRoute(to)

  routeTransitionNameStore.setName('slide-fadein-left')
  const toLevel = to.meta.level || 0
  const formLevel = from.meta.level || 0

  if (toLevel > formLevel) routeTransitionNameStore.setName('slide-fadein-right')
  else if (toLevel < formLevel) routeTransitionNameStore.setName('slide-fadein-left')
  else routeTransitionNameStore.setName('absolute-layout')

  next()
})

router.afterEach(() => {
  NProgress.done()
})
